package com.gem.netty.message;

import com.gem.entity.Game;
import com.gem.entity.Gem;
import com.gem.enums.GameRoundStageType;
import com.gem.enums.GemType;
import com.gem.enums.MessageColorType;
import com.gem.factory.game.GameFactory;
import com.gem.netty.entity.ClientContent;
import com.gem.netty.entity.CurrentUser;
import com.gem.netty.message.swap.AbstractSwapBase;
import com.gem.netty.message.swap.GetGem;
import com.gem.netty.service.common.IsCurrentUserRoundService;
import com.gem.netty.service.gem.GetGemVerifyService;
import com.gem.netty.service.gem.IsExistGemService;
import com.gem.netty.service.gem.IsGetGemRoundService;
import com.gem.netty.service.gem.IsPutRoundGemService;
import com.gem.netty.session.GroupSessionFactory;
import com.gem.pipeline.BusinessProcess;
import com.gem.pipeline.ProcessContext;
import com.gem.pipeline.ProcessController;
import com.gem.utils.R;
import io.netty.channel.Channel;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.http.websocketx.TextWebSocketFrame;

import java.util.*;

/**
 * Author: Xhy
 * CreateTime: 2022-09-22 09:59
 * 玩家拿宝石阶段
 */
public class GetGemMessage implements Message {

    @Override
    public void handler(ClientContent content) {
        Game game = GameFactory.get(content.getRoomId());
        ChannelHandlerContext ctx = content.getCtx();
        String roomId = game.getRoomId();

        // 获取当前回合用户
        CurrentUser user = game.getUsers().get(game.getCurrentIndex());
        // 将请求中的宝石信息存入当前用户回合信息
        user.setCurrentGetGemInfo(content.getContent().getGem());

        AbstractSwapBase getGem = new GetGem();
        R r = getGem.doSwap(content);
        if (!r.getState()){
            ctx.writeAndFlush(new TextWebSocketFrame(r.toString()));
            return;
        }
        // 获取当前用户拿宝石
        user.setCurrentGetGemInfo(content.getContent().getGem());
        Integer currentGetGem = user.getCurrentGetGemInfo();
        // 修改状态
        tryUpdateState(user,currentGetGem);
        // 用户增加对应宝石数量
        putGem(user,currentGetGem);
        // 游戏宝石减少
        reduceGameInGem(game,currentGetGem);
        // 增加用户总宝石数量
        addGemNumber(user);
        // 告知其他用户拿取宝石信息
        List<Channel> channels = GroupSessionFactory.getGroupSession().getMembersChannel(roomId);
        GameFactory.notifyUsers(channels,R.ok().data("color", MessageColorType.GET_GEM.type).message(user.getName()+" 玩家拿取:"+ Gem.getGemInfo(currentGetGem)).type(content.getType()),roomId);
    }




    /**
     * 减少宝石库宝石
     * @param game
     * @param currentGetGem
     */
    private void reduceGameInGem(Game game,Integer currentGetGem){
        // 获取当前用户选择的宝石
        game.getGems().put(currentGetGem,game.getGems().get(currentGetGem)-1);
    }


    /**
     * 增加手里宝石数量
     * @param user
     */
    private void addGemNumber(CurrentUser user){
        user.setGetGemNumber(user.getGetGemNumber()+1);
    }

    /**
     * 宝石入库
     * @param user
     * @param gem
     */
    private void putGem(CurrentUser user,Integer gem){
        Map<Integer, Integer> gems = user.getGems();
        // 是否拿的是黄金:加状态
        if (gem == 6){
            user.setTSFlag(true);
        }
        if (gems.containsKey(gem)){
            gems.put(gem,gems.get(gem)+1);
        }else {
            gems.put(gem,1);
        }
    }

    /**
     * 尝试修改状态
     * @param user 当前用户
     * @param currentGem 当前拿的宝石
     */
    private void tryUpdateState(CurrentUser user,Integer currentGem){
        // 拿取是黄金宝石则进入换牌阶段
        // 存在俩个相同或者回合宝石库==3个 进入换牌阶段
        if (currentGem == GemType.GOLD.type || user.getCurrentGemState().get(currentGem) == 2 || user.getCurrentGemState().size() == 3){
            user.setState(GameRoundStageType.SWAP_CARD.type);
        }
    }

}
